using System;
using System.Collections.Generic;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;

namespace BPCSFileDaemon.FileFactory
{
    public class ExchangeRateFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 174;

        public ExchangeRateFileFactory(string path, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(path, DBConnectionString, DatabaseType)
        {
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawEmployeeDataManager CurDataManager = new RawEmployeeDataManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();

                        // start to analyze
                        for (int x = 0; x < this.FileLines.Count; x++)
                        {
                            try
                            {
                                string CurrentLine = this.FileLines[x].ToString();

                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 174 is the only length
                                {
                                    string CountryID = CurrentLine.Substring(0, 2).Trim();    // size: 2
                                    string CompanyCode = CurrentLine.Substring(2, 2).Trim();    // size: 2
                                    string EmployeeID = CurrentLine.Substring(4, 6).Trim();    // size: 6
                                    string ActualName = CurrentLine.Substring(10, 50).Trim();      // size: 50
                                    string LocalName = CurrentLine.Substring(60, 60).Trim();      // size: 60
                                    string EmailID = CurrentLine.Substring(120, 50).Trim();    // size: 50
                                    string CostCenter = CurrentLine.Substring(170).Trim();    // size: 10

                                    CurDataManager.AddData(CountryID, CompanyCode, EmployeeID, ActualName, LocalName, EmailID, CostCenter);

                                    /*
                                    Console.WriteLine("CountryID: " + CountryID);
                                    Console.WriteLine("CompanyCode: " + CompanyCode);
                                    Console.WriteLine("EmployeeID: " + EmployeeID);
                                    Console.WriteLine("ActualName: " + ActualName);
                                    Console.WriteLine("LocalName: " + LocalName);
                                    Console.WriteLine("EmailID: " + EmailID);
                                    Console.WriteLine("CostCenter: " + CostCenter);
                                    Console.WriteLine();
                                    */

                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("[EmployeeFileFactory.ReadFile]" + ex.Message);
                            }
                        }   // for

                        // inject now
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (CurDataManager.ExecuteDataInject())
                            {



                            }
                        }
                    }
                }
            }

            return smooth;
        }
    }
}
